package cn.com.eado.crm.v.fragment


import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.navigation.Navigation
import androidx.recyclerview.widget.LinearLayoutManager
import cn.com.eado.crm.R
import cn.com.eado.crm.databinding.FragmentWeeklyBinding
import cn.com.eado.crm.model.LoadStatus
import cn.com.eado.crm.model.RetryCallback
import cn.com.eado.crm.v.adapter.RVCompanyAdapter
import cn.com.eado.crm.vm.CompanyViewModel
import cn.com.eado.preado.app.utils.autoCleared
import com.getbase.floatingactionbutton.FloatingActionButton
import kotlinx.android.synthetic.main.fragment_weekly.*

/**
 * A simple [Fragment] subclass.
 */
class WeeklyFragment : BaseBindingFragment<FragmentWeeklyBinding>(R.layout.fragment_weekly) {

    private var vm by autoCleared<CompanyViewModel>()
    private var adapter by autoCleared<RVCompanyAdapter>()

    override fun initTheFragment() {
        initView()
        initViewModel()
        initBinding()
        loadData()
    }

    private fun initView() {
        initTitleBar()
        initOptionMenu()
        initRecyclerView()
    }

    private fun initTitleBar() {
        val title = arguments?.getString("name")?:"XXX管理"
        titleBar6.setTitle(title)
    }

    private fun initOptionMenu(){
        fab_menu5.findViewById<FloatingActionButton>(R.id.fab_1).setOnClickListener {
            val bundle = Bundle()
            bundle.putString("name", "新增周报")
            Navigation.findNavController(view!!).navigate(R.id.action_weeklyFragment_to_weeklyDetailFragment, bundle)
        }

        fab_menu5.findViewById<FloatingActionButton>(R.id.fab_2).setOnClickListener {
            val bundle = Bundle()
            bundle.putString("name", "编辑周报")
            Navigation.findNavController(view!!).navigate(R.id.action_weeklyFragment_to_weeklyDetailFragment, bundle)
        }
    }


    private fun initRecyclerView() {
        recyclerView3.layoutManager = LinearLayoutManager(activity)
        val adapter = RVCompanyAdapter({ view, pos, bean ->  //click
            //
        },{ pos, bean ->  //long click
            //
        })
        recyclerView3.adapter = adapter
        this.adapter = adapter
    }

    private fun initViewModel(){
        vm = ViewModelProviders.of(this).get(CompanyViewModel::class.java)
//        vm.result.observe(this, Observer {
//            if (it.status == LoadStatus.SUCCESS){
////                Log.e("tag", "it=${it.data}")
//            }
//        })
    }

    private fun initBinding(){
        binding.lifecycleOwner = viewLifecycleOwner
//        binding.result = vm.result
        binding.callback = object : RetryCallback {
            override fun retry() {
                vm.refresh()
            }
        }
    }

    private fun loadData(){
        vm.setLoadCompany(true)
    }


}
